﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JVP.Gateway;
using JVP.Infrastructure;
using System.Data.SqlClient;
using System.Collections;


namespace JeevanVaisya
{
    public partial class Bond : Form
    {
        #region Variable Initialization
        string userName;
        string se; ///this variable for save or edit
        Gateway g = new Gateway();
        Gateway gateway = new Gateway();
        SqlDataReader dr = null;
        int i=0;
        #endregion

        #region Constructor

        public Bond(string userID)
        {
            InitializeComponent();
            //textBox8.LostFocus += new EventHandler(textBox8_LostFocus);
            //comboBox7.GotFocus += new EventHandler(comboBox7_GotFocus);
            cbPlan.SelectedValueChanged += new EventHandler(cbPlan_SelectedValueChanged);
            lstScheme.ItemCheck += new ItemCheckEventHandler(lstScheme_ItemCheck);
            btnSave.Click += new EventHandler(btnSave_Click);
            txtCode.Leave += new EventHandler(txtCode_Leave);
            initializeFunction();
            userName = userID;
            getOfficeStaffDetails(userID);
            groupBox3.Visible = false;
        }

        private void getOfficeStaffDetails(string userID)
        {
            SqlDataReader dr = gateway.getOfficeStaffDetails(userID);
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    lblUserName.Text = dr["Name"].ToString();
                    lblDOB.Text = Convert.ToDateTime(dr["DateofBirth"].ToString()).ToLongDateString();
                    lblBranch.Text = dr["BranchCity"].ToString();
                    lblBranch.Tag = dr["BranchID"];
                    lblDate.Text = DateTime.Now.ToLongDateString();
                }
            }
            dr.Close();
            dr.Dispose();
        }

        private void initializeFunction()
        {
            cbGender.Text = "--Select Gender--";
            cbGender.Items.Add("Male");
            cbGender.Items.Add("Female");
            ArrayList fatherOf = new ArrayList();
            fatherOf.Add(new GeneralDataTypes("Son Of", "S/O"));
            fatherOf.Add(new GeneralDataTypes("Daughter Of", "D/O"));
            fatherOf.Add(new GeneralDataTypes("Wife Of", "W/O"));
            fatherOf.Add(new GeneralDataTypes("Care Of", "C/O"));
            cbFatherof.DataSource = fatherOf;
            cbFatherof.DisplayMember = "PlanName";
            cbFatherof.ValueMember = "PlanID";
        }

        #endregion

        #region Get Upline of Employee

        void txtCode_Leave(object sender, EventArgs e)
        {
            try
            {
                lbUpline.DataSource = null;
                lbUpline.Items.Clear();                
                ArrayList arrUpLine = new ArrayList();
                dr = gateway.getUpline(txtCode.Text);
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        if (dr["EmpCode"].ToString() == txtCode.Text)
                            txtIntro.Text = dr["IntroducerCode"].ToString();
                        arrUpLine.Add(new GeneralDataTypes(dr["EmpCode"] + "(" + dr["Rank"] + ") - " + dr["DesignationName"], Convert.ToInt32(dr["EmpCode"])));
                    }
                    lbUpline.DisplayMember = "BranchName";
                    lbUpline.ValueMember = "BranchID";
                    lbUpline.DataSource = arrUpLine;
                }
                else
                    MessageBox.Show("No Such Code Number");
                dr.Dispose();
                dr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Save Policy

        void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (validateForm())
                {
                    DataSet dsUpLine = new DataSet();
                    dsUpLine = getPercentages();
                    if (gateway.addPolicy(Convert.ToInt32(lstScheme.SelectedValue), Convert.ToInt32(txtCSS.Text), txtName.Text.Trim(),
                        txtAge.Text.Trim(), cbGender.SelectedText, cbFatherof.SelectedText, txtFatherName.Text.Trim(), txtNName.Text.Trim(), txtNAge.Text.Trim(),
                        txtNRelation.Text.Trim(), txtAddress.Text.Trim(), dtComm.Value, Convert.ToInt32(txtCode.Text.Trim()), dsUpLine, userName, Convert.ToInt32(lblBranch.Tag)))
                    {
                        MessageBox.Show("New Policy Created Successfully");
                    }
                }
                else
                    MessageBox.Show("Pls Enter Clear Form Details");
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private DataSet getPercentages()
        {
            DataSet upLine = new DataSet();
            upLine.Tables.Add("EmpList");
            upLine.Tables["EmpList"].Columns.Add("EmpID");
            upLine.Tables["EmpList"].Columns.Add("EmpAmt");
            upLine.Tables["EmpList"].AcceptChanges();
            dr = gateway.getUpline(txtCode.Text);
            SqlDataAdapter da = gateway.getRankList();
            DataSet ds = new DataSet();
            da.Fill(ds);
            int i = -1;
            while (dr.Read())
            {
                double perCentage = 0;
                DataRow dataRow = upLine.Tables["EmpList"].NewRow();
                do
                {
                    i++;
                    perCentage += Convert.ToDouble(ds.Tables[0].Rows[i]["IYear"]);
                } while (Convert.ToInt32(ds.Tables[0].Rows[i]["Rank"]) != Convert.ToInt32(dr["Rank"]));
                dataRow["EmpID"] = dr["EmpCode"].ToString();
                dataRow["EmpAmt"] = perCentage;
                upLine.Tables["EmpList"].Rows.Add(dataRow);
            }
            return upLine;
        }

        private bool validateForm()
        {
            if (Convert.ToInt32(cbPlan.SelectedValue) != -1 && Convert.ToInt32(lstScheme.SelectedValue) != 0 && txtCSS.Text.Trim() != "" && txtName.Text.Trim() != "" && txtAddress.Text.Trim() != "" &&
                txtAge.Text.Trim() != "" && cbGender.SelectedText != "--Select Gender --" &&  cbFatherof.SelectedValue.ToString() != "" && txtCode.Text.Trim() != "" && txtFatherName.Text.Trim() != "" && txtNAge.Text.Trim() != "" &&
                txtNName.Text.Trim() != "" && txtNRelation.Text.Trim() != "" && (int)lbUpline.Items.Count != 0)
                return true;
            else
                return false;
        }

        #endregion

        #region To Change Only One Item to Select in the list box

        void lstScheme_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
                for (int ix = 0; ix < lstScheme.Items.Count; ++ix)
                    if (e.Index != ix) lstScheme.SetItemChecked(ix, false);
        }
        #endregion

        #region To Load Available Schemes in the list box

        void cbPlan_SelectedValueChanged(object sender, EventArgs e)
        {
            if (i != 1)
            {
                lstScheme.DataSource = null;
                dr = gateway.getScheme(cbPlan.SelectedValue.ToString());
                ArrayList SchemeDetails = new ArrayList();
                if (dr.HasRows)
                {
                    while (dr.Read())
                        if(Convert.ToInt32(dr["SchemeDues"])!=0)
                            SchemeDetails.Add(new GeneralDataTypes(String.Format("{0:C}", Convert.ToDouble(dr["Amount"].ToString())), dr["SchemeID"].ToString()));
                        else
                            SchemeDetails.Add(new GeneralDataTypes(String.Format("{0:C}", Convert.ToDouble(dr["Consideration"].ToString())), dr["SchemeID"].ToString()));
                        //SchemeDetails.Add(new GeneralDataTypes(dr["PlanYear"].ToString() + " - " + dr["SchemeDues"].ToString() + " Dues - " + String.Format("{0:C}", Convert.ToDouble(dr["Amount"].ToString())) + " - " + String.Format("{0:C}", Convert.ToDouble(dr["Consideration"].ToString())), dr["SchemeID"].ToString()));
                    lstScheme.DataSource = SchemeDetails;
                    lstScheme.DisplayMember = "PlanName";
                    lstScheme.ValueMember = "PlanID";
                }
            }
        }

        #endregion

        #region Load Event of the Bond Preparation

        private void Bond_Load(object sender, EventArgs e)
        {
            //To Get the available plans from database
            dr = gateway.getPlans();
            cbPlan.DataSource = null;
            ArrayList PlanDetails = new ArrayList();
            PlanDetails.Add(new GeneralDataTypes("--Select Plan--", "-1"));
            if (dr.HasRows)
            {
                i = 1;
                while (dr.Read())
                    PlanDetails.Add(new GeneralDataTypes(dr["PlanName"].ToString() + " - " + dr["PlanTerm"].ToString(), dr["PlanID"].ToString()));
                cbPlan.DataSource = PlanDetails;
                cbPlan.DisplayMember = "PlanName";
                cbPlan.ValueMember = "PlanID";
                i = 0;
                dr.Close();
                dr.Dispose();
                cbPlan.Text = "--Select Plan--";
            }
        }

        #endregion

    }
}





































        //     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        //     {
        //         comboBox2.Items.Clear();
        //         comboBox3.Items.Clear();
        //         if (comboBox1.SelectedItem.ToString().Substring(0,5) == "PlanA")
        //         {
        //             comboBox2.Items.Add("2.5");
        //             comboBox4.Text = "";
        //             comboBox5.Text = "";
        //         }
        //         else if (comboBox1.SelectedItem.ToString().Substring(0,5) == "PlanB")
        //         {
        //             comboBox2.Items.Add("2");
        //             comboBox2.Items.Add("4");
        //             comboBox2.Items.Add("6");
        //             comboBox4.Text = "";
        //             comboBox5.Text = "";
        //         }
        //         else
        //         {
        //             comboBox2.Items.Add("5");
        //             comboBox4.Text = "";
        //             comboBox5.Text = "";
        //         }
        //         if (comboBox1.Text.Substring(0,5) == "PlanA" || comboBox1.Text.Substring(0,5) == "PlanC")
        //         {
        //             comboBox3.Items.Add("M");
        //             comboBox3.Items.Add("Q");
        //             comboBox3.Items.Add("H");
        //             comboBox3.Items.Add("Y");
        //         }
        //         else
        //             comboBox3.Items.Add("Lumsum");
        //         addlist();
        //     }

        //     private void cleartextbox()
        //     {
        //         textBox8.Text = "";
        //         textBox9.Text = "";
        //         textBox10.Text = "";
        //         textBox11.Text = "";
        //         textBox12.Text = "";
        //         textBox13.Text = "";
        //         textBox14.Text = "";
        //         textBox15.Text = "";
        //         textBox16.Text = "";
        //         textBox17.Text = "";
        //         textBox18.Text = "";
        //         textBox19.Text = "";

        //     }
        //     private void addlist()
        //     {
        //         string s1,s2,s3,s4,s5,s;
        //         st = "";
        //         checkedListBox1.Items.Clear();
        //         s1 =comboBox1.Text.Substring(0,5) ;
        //         s2 = comboBox2.Text;
        //         s3 = comboBox1.Text.Substring(6, comboBox1.Text.Length - 6);
        //         s4 = comboBox4.Text;
        //         s5 = comboBox5.Text;
        //         s = g.getscheme(s1,s2,s3,s4,s5);
        //        for (int i = 0; i <= s.Length-1 ; i++)
        //         {
        //             if (s.Substring(i, 1) != "|")
        //                 st = st + s.Substring(i, 1);
        //             else
        //             {
        //                checkedListBox1.Items.Add(st);
        //                st = "";
        //             }
        //         }         
        //     }
        //     private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        //     {
        //        addlist();
        //     }
        //     private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        //     {
        //         string[] ins, cos = new string[50];
        //         int i; 
        //         string s;
        //         addlist();
        //         comboBox4.Items.Clear();
        //         comboBox5.Items.Clear();
        //         ins = g.getins(comboBox1.Text, comboBox2.Text, comboBox3.Text);
        //         if (ins[0]!=null)
        //         {
        //             for (i = 0; i <= ins.Length - 1; i++)
        //             {
        //                 s = ins[i];
        //                if (s==null) break;
        //                if (ins[i+1] != s || ins[i+1]==null )
        //                {
        //                    if (s.Substring(0, s.IndexOf('|')) != "Null")
        //                        comboBox4.Items.Add(s.Substring(0, s.IndexOf('|')));
        //                    comboBox5.Items.Add(s.Substring(s.IndexOf('|', 1) + 1, s.Length - s.IndexOf('|', 1) - 1));
        //                }
        //                 ///for (int j = 0; j <= s.Length - 1; j++)
        //                 /// {
        //                 ///  if (s.Substring(j,s.IndexOfAny('|') ) != "|")
        //                 ////         st = st + s.Substring(j, 1);
        //                 ///     else
        //                 ///  {
        //                 ///    combobox4.Items.Add(st);
        //                 ///    st = "";
        //                 /// }
        //                 ///}         

        //             }
        //             if (i > 0)
        //             {
        //                 s = ins[i - 1];
        //                 if (s.Substring(0, s.IndexOf('|')) == "Null")
        //                     comboBox4.Items.Add("Lumsum");
        //             }

        //         }
        //     }
        //     private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        //     {
        //         addlist();
        //     }

        //     private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        //     {
        //         addlist();
        //     }

        //     private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        //     {
        //         textBox20.Text  = checkedListBox1.Text.Substring(checkedListBox1.Text.Length-5,3);
        //         //MessageBox.Show(getamtpaid(checkedListBox1.Text));
        //     }

        //  void textBox8_LostFocus(object sender, EventArgs e)
        //     {
        //         addagentcode();
        //     }

        //  void addagentcode()
        //  {

        //      string[] s = new string[20];
        //      string c = textBox8.Text;
        //      int i = 0;
        //      if (g.isvalidemp(c) == true)
        //      {
        //          s = g.getdownlink(c);
        //          ///for display in rank vice
        //          while (s[i] != null)
        //          {
        //              // MessageBox.Show(s[i]);
        //              switch (g.getrank(s[i]))
        //              {
        //                  case 1:
        //                      textBox11.Text = s[i];
        //                      break;
        //                  case 2:
        //                      textBox12.Text = s[i];
        //                      break;
        //                  case 3:
        //                      textBox13.Text = s[i];
        //                      break;
        //                  case 4:
        //                      textBox14.Text = s[i];
        //                      break;
        //                  case 5:
        //                      textBox15.Text = s[i];
        //                      break;
        //                  case 6:
        //                      textBox16.Text = s[i];
        //                      break;
        //                  case 7:
        //                      textBox17.Text = s[i];
        //                      break;
        //                  case 8:
        //                      textBox18.Text = s[i];
        //                      break;
        //                  case 9:
        //                      textBox19.Text = s[i];
        //                      break;
        //              }
        //              i++;
        //          }
        //          textBox9.Text = s[1];//for intro
        //          textBox10.Text = s[i - 1];//for unit
        //      }
        //      else
        //      {
        //          MessageBox.Show("Invalid Code");
        //          cleartextbox();

        //      }
        //  }

        //void button1_Click(object sender, EventArgs e)
        //  {
        //      int receipt,cssno;
        //      string[] str = new string[20];
        //      string entryp,year,date;
        //      double I,II,III, Lum,amt;
        //      //str = getamtpaid(checkedListBox1.Text);
        //      //I = float.Parse(str[6]);
        //      if (comboBox1.Text != "" && checkedListBox1.SelectedIndex >= 0 && textBox8.Text != "")
        //      {
        //          entryp = g.getperson();
        //          receipt = g.getreceiptno("PolicyDetails");
        //          g.insertpolicy(long.Parse(textBox1.Text), textBox2.Text, int.Parse(textBox20.Text), comboBox1.Text.Substring(6, comboBox1.Text.Length - 6), dateTimePicker1.Text.ToString(), int.Parse(textBox6.Text), textBox22.Text.ToString(), richTextBox1.Text, textBox5.Text.ToString(), textBox4.Text, textBox7.Text.ToString(), int.Parse(textBox3.Text.ToString()), textBox8.Text.ToString(), entryp, int.Parse(textBox21.Text.ToString()), receipt, se);
        //          receipt = g.getreceiptno("RenewalDetails");
        //          str = getamtpaid(checkedListBox1.Text);
        //          if (str[6] != "Null")
        //          g.insertrenewal(receipt, long.Parse(textBox1.Text), double.Parse(str[6].ToString()), int.Parse(textBox20.Text), int.Parse(textBox21.Text),0.0,0.0, entryp, dateTimePicker1.Text.ToString(), se);
        //          if (int.Parse(DateTime.Now.Month.ToString()) > 3)
        //              year = DateTime.Now.Year.ToString() + "-" + (int.Parse(DateTime.Now.Year.ToString()) + 1).ToString();
        //          else
        //              year = (int.Parse(DateTime.Now.Year.ToString()) - 1).ToString() + "-" + DateTime.Now.Year.ToString();
        //          //year = DateTime.Now.Month();
        //          receipt = g.getreceiptno("SalaryDetails");
        //          if (str[6] != null)
        //          {
        //              I = float.Parse(str[6]);
        //              II = 0.0;
        //              III = 0.0;
        //              Lum = 0.0;

        //          }
        //          else
        //          {
        //              I = 0.0;
        //              II = 0.0;
        //              III = 0.0;
        //              Lum = float.Parse(str[4]);
        //          }
        //          //date = dateTimePicker1.Text.Substring(0, dateTimePicker1.Text.IndexOf('/')).ToString();
        //          //dateTimePicker1.Format = dateTimePicker1.CustomFormat;

        //          g.insertsalary(receipt, textBox8.Text.ToString(), dateTimePicker1.Value.Month.ToString(), year, I, II, III, Lum, double.Parse(str[8]), "save");
        //          g.inserttem(long.Parse(textBox1.Text.ToString()));
        //          clearbond();
        //      }
        //      else
        //      {
        //          MessageBox.Show("Not perpare select");
        //      }
        //  }

        //public string[] getamtpaid(string s2)
        //{
        //    string[] str = new string[10];
        //    string s1="";
        //    int i,l=0;
        //    for (i = 0; i <= s2.Length-1; i++)
        //    {
        //        if (s2.Substring(i, 1) != " ")
        //            s1 += s2.Substring(i, 1);
        //        else
        //        {
        //            str[l] = s1;
        //            s1 = "";
        //            l++;
        //        }
        //    }
        //    return str;

        //}
        //public void clearbond()
        //{
        //    comboBox1.Items.Clear();
        //    comboBox2.Items.Clear();
        //    comboBox2.Text = "";
        //    comboBox3.Items.Clear();
        //    comboBox3.Text = "";
        //    comboBox4.Items.Clear();
        //    comboBox4.Text = "";
        //    comboBox5.Items.Clear();
        //    comboBox5.Text = "";
        //    checkedListBox1.Items.Clear();
        //    textBox1.Text = "";
        //    textBox2.Text = "";
        //    textBox3.Text = "";
        //    textBox4.Text = "";
        //    textBox5.Text = "";
        //    textBox6.Text = "";
        //    textBox7.Text = "";
        //    textBox8.Text = "";
        //    textBox9.Text = "";
        //    textBox10.Text = "";
        //    textBox11.Text = "";
        //    textBox12.Text = "";
        //    textBox13.Text = "";
        //    textBox14.Text = "";
        //    textBox15.Text = "";
        //    textBox16.Text = "";
        //    textBox17.Text = "";
        //    textBox18.Text = "";
        //    textBox19.Text = "";
        //    textBox20.Text = "";
        //    textBox21.Text = "";
        //    textBox22.Text="";
        //    richTextBox1.Text = "";
        //    se = "save";
        //    comboBox1.Items.Add("PlanA M");
        //    comboBox1.Items.Add("PlanA Q");
        //    comboBox1.Items.Add("PlanA H");
        //    comboBox1.Items.Add("PlanA Y");
        //    comboBox1.Items.Add("PlanB Lumsum");
        //    comboBox1.Items.Add("PlanC M");
        //    comboBox1.Items.Add("PlanC Q");
        //    comboBox1.Items.Add("PlanC H");
        //    comboBox1.Items.Add("PlanC Y");
        //    comboBox1.Items.Add("PlanD Lumsum");
        //    textBox21.Text = g.getcssno("PolicyDetails").ToString();
        //    textBox1.Text = g.getjvm().ToString();
        //}

        //private void button2_Click(object sender, EventArgs e)
        //{
        //    int len,i;
        //    len = g.getrowcount();
        //   string[] id = new string[len];
        //   textBox1.Visible = false;
        //   comboBox6.Visible = true;
        //   //button3.Visible = true;
        //   id = g.getcustomerid();
        //   se = "Edit";
        //   comboBox6.Items.Clear();
        //    for (i = 0; i < len-1; i++)
        //       comboBox6.Items.Add(id[i]);

        //}

        //private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string[] str = new string[20];
        //    str = g.geteditdet(long.Parse(comboBox6.Text));
        //    textBox1.Visible = true;
        //    textBox1.Text = comboBox6.Text;
        //    comboBox6.Visible = false;

        //    if (int.Parse(str[2]) >= 111 && int.Parse(str[2]) <= 120)
        //        comboBox1.Text = "PlanA " + str[3];
        //    else if (int.Parse(str[2]) >= 211 && int.Parse(str[2]) <= 231)
        //        comboBox1.Text = "PlanB " + str[3];
        //    else if (int.Parse(str[2]) >= 311 && int.Parse(str[2]) <= 320)
        //        comboBox1.Text = "PlanC " + str[3]; 
        //    else
        //        comboBox1.Text = "PlanD " + str[3];
        //    comboBox3.Text = str[3];
        //    addlist();
        //    textBox20.Text = str[2];
        //    textBox2.Text = str[1];
        //    textBox6.Text = str[5];
        //    textBox22.Text = str[6];
        //    textBox5.Text = str[8];
        //    textBox7.Text = str[10];
        //    textBox3.Text = str[11];
        //    textBox4.Text = str[9];
        //    richTextBox1.Text = str[7];
        //    dateTimePicker1.Text = str[4];
        //    textBox8.Text = str[12];
        //    addagentcode();
        //}

        //private void button3_Click(object sender, EventArgs e)
        //{
        //    //Application.Run(new Bond());
        //}

        //void comboBox7_GotFocus(object sender, EventArgs e)
        //{
        //    string[] printid = new string[200];
        //    comboBox7.Items.Clear();
        //    printid = g.getprintid();
        //    for (int i = 0; printid[i]!=null; i++)
        //    comboBox7.Items.Add(printid[i]);
        //}

        //private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //}
        // }
  //  }
//}
